package edu.com.wjt219971332.controller;

import edu.com.wjt219971332.entity.Matchup;
import edu.com.wjt219971332.entity.Result;
import edu.com.wjt219971332.entity.Singer;
import edu.com.wjt219971332.service.MatchupService;
import edu.com.wjt219971332.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@Controller
@RequestMapping("match")
public class matchupController {
    @Autowired
    private MatchupService matchupService;
    @RequestMapping("show")
    public String show(){
        return "matuch/querymatuch";
    }

    @GetMapping
    @ResponseBody
    public Result queryByPage(Matchup matchup, @RequestParam("page") Integer page, @RequestParam("size")Integer size, @RequestParam(value = "sort", required = false)String sort) {
        PageRequest pageable = PageRequest.of(page-1, size);
        Page<Matchup> result = this.matchupService.queryByPage(matchup, pageable);
        return Result.ok("获取成功！",  result.getTotalElements(), result.get().collect(Collectors.toList()));
    }
    @RequestMapping("/select")
    @ResponseBody
    public Result select(@RequestParam("name") String name){
        return  Result.ok("操作成功！", this.matchupService.queryByusername(name));
    }

    @RequestMapping("/insertInit")
    public String registerInit(){
        return "matchup/insert";
    }

    @RequestMapping("/insert")
    public String register(@ModelAttribute("matchup") Matchup matchup){
        matchupService.insert(matchup);
        return "redirect: ../login/loginInit";
    }

    @RequestMapping("/updateInit/{singerid}")
    public String updateInit(@PathVariable("matchup") Integer matchupid
            , Model model){
        Matchup matchup = matchupService.queryById(matchupid);
        model.addAttribute("singer", matchup);
        return  "singer/update";
    }
    @RequestMapping("/update")
    @ResponseBody
    public Result update(@ModelAttribute("matchup") Matchup matchup){
        return Result.ok("修改成功",this.matchupService.update(matchup));
    }

    @RequestMapping("/deleteInit/{matchupid}")
    @ResponseBody
    public String deleteInit(@PathVariable("matchupid") Integer matchupid){
        matchupService.deleteById(matchupid);
        return "redirect:/login/loginshow";
    }
}
